package com.rent.view;
/**
 * 菜单
 * @author xin
 *
 */

import java.io.Console;
import java.util.Scanner;

import org.apache.log4j.Logger;

import com.rent.Impl.UserDaoImpl;
import com.rent.bean.User;
import com.rent.dao.UserDao;
import com.rent.test.Test2;
import com.rent.utils.ViewUtils;

public class Menu {
	//用户接口
	UserDao userDao = new UserDaoImpl();
	ViewUtils v = new ViewUtils();
	Logger log=Logger.getLogger(Menu.class);
	Scanner sn = new Scanner(System.in);
	public void init() {
		System.out.println("欢迎来到神州租车---------神州行，我看行");
		System.out.println("请输入选项：1.登录 2.注册  (输入其它退出)");
		int input =0;
		try {
			input = sn.nextInt();
			switch (input) {
			case 1:
				login();
				break;
			case 2:
				register();
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 登录
	 */
	public void login() {
		v.printTitle("神州租车用户登录界面");
		try {
			System.out.println("请输入用户名：");
			String name = sn.next();
			System.out.println("请输入密码");
			String pwd = sn.next();
			User user = userDao.sele_nameAndpwd(name, pwd);
			if(user!=null) {
				System.out.println("密码正确，登录成功");
				v.printTitle("欢迎进入神州租车");
				//判断用户
				switch (user.getPower()) {
				case 1:
					//普通用户
					new OrdinaryMenu().menu_ordinary(user);;
					break;
				case 2:
					//vip用户
					new VipMenu().menu_vip(user);
					
					break;
				case 3:
					//admin
					new AdminMenu().menu_admin(user);
					break;
				}
			}else {
				System.out.println("登录失败");
			}
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		} 
	}
	/**
	 * 注册
	 */
	public void register() {
		User u = new User();
		v.printTitle("神州租车用户注册界面");
		try {
			System.out.print("请输入用户名：");
			u.setUname(sn.next());
			//判断此用户名是否已注册
			boolean flg = true;
			do {
				if(userDao.selectByName(u.getUname())!=null) {
					System.out.print("用户已被注册，请重新输入:");
					u.setUname(sn.next());
				}else {
					flg = false;
				}
			}while(flg);
			System.out.println();
			System.out.println("请输入密码：");
			u.setUpassword(sn.next());
			System.out.println("确认密码：");
			if(u.equals(sn.next())) {
				System.out.println("密码确认失败，是否重新注册？（1：是 2：否）");
				if(sn.nextInt()==1) {
					init();
				}else {
					System.out.println("欢迎下次登陆");
				}
			}
			System.out.println("请输入姓名：");
			u.setIdname(sn.next());
			System.out.println("请输入身份证号：");
			u.setIdcard(sn.next());
			System.out.println("请输入手机号：");
			u.setPhonenumber(sn.next());
			userDao.add(u);
			System.out.println("注册成功！请登录");
			login();
		} catch (Exception e) {
			log.error(e);
			System.out.println("系统可能有些小问题");
		} 
		
	}
}